package com.mk.cloud.common.service.core;

import act.apidoc.Description;
import act.controller.Controller;
import act.inject.DefaultValue;
import com.alibaba.fastjson.JSON;
import com.mk.cloud.common.config.AppConfig;
import com.mk.cloud.common.domain.CpMsg;
import com.mk.cloud.common.domain.CpMsgTemplete;
import com.mk.cloud.common.enums.GlobalValues;
import com.mk.cloud.common.exception.UserException;
import com.mk.cloud.common.interceptor.ITransacte;
import com.mk.cloud.common.interceptor.cookie.CookieService;
import com.mk.cloud.common.interceptor.cookie.ICLogin;
import com.mk.cloud.common.util.DateUtils;
import org.osgl.util.S;import com.mk.cloud.common.util.S2;
import org.beetl.sql.core.engine.PageQuery;
import org.osgl.mvc.annotation.*;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller("backend/cpMsgTemplete")
public class CpMsgTempleteService extends CookieService {
    @Inject
    private CpMsgTemplete.Mapper cpMsgTempleteMapper;

    @GetAction("pageList.json")
    @Description("查询所有有效的消息列表")
    @With(ICLogin.class)
    public PageQuery<CpMsgTemplete> pageList(
            @Description("筛选条件:消息内容") String msg,
            @Description("筛选条件:消息类型") String msgtype,
            @Description("筛选条件:开始时间") String bdate,
            @Description("筛选条件:结束时间") String edate,
            @Description("当前第几页") @DefaultValue("1") Integer currentPage,
            @Description("每页多少条") @DefaultValue("10") Integer pageSize){
        PageQuery<CpMsgTemplete> query = new PageQuery<>();
        query.setPageSize(pageSize);
        query.setPageNumber(currentPage);
        Map<String,String> map = new HashMap<>();
        map.put("msg",msg);
        if(S.isEmpty(msgtype)){
            map.put("msgtype",null);
        }else{
            map.put("msgtype",msgtype);
        }
        map.put("bdate",bdate);
        map.put("edate",edate);
        query.setParas(map);
        cpMsgTempleteMapper.getSQLManager().pageQuery("cpMsgTemplete.select_list_msg_templete",CpMsgTemplete.class,query);
        return query;
    }


    @GetAction("form.json")
    @Description("消息对象")
    @With(ICLogin.class)
    public CpMsgTemplete form(@Description("消息主键") String mtid){
        CpMsgTemplete cpMsgTemplete = null;
        if(S.isNotBlank(mtid)){
            cpMsgTemplete = cpMsgTempleteMapper.selectSingleById(mtid);
        }
        return cpMsgTemplete;
    }

    @PostAction("add.json")
    @Description("消息新增")
    @With({ICLogin.class, ITransacte.class})
    public CpMsgTemplete add(@Description("消息对象") CpMsgTemplete cpMsgTemplete){
        cpMsgTemplete.setMtid(S2.UUID());
        cpMsgTemplete.setMtime(DateUtils.getTimeNow());
        cpMsgTemplete.setIssend(GlobalValues.FLAG_NO.value());
        cpMsgTemplete.setMurl(AppConfig.msg.path.get());
        cpMsgTempleteMapper.insertTemplate(cpMsgTemplete);
        return cpMsgTemplete;
    }

    @PutAction("update.json")
    @Description("消息修改")
    @With({ICLogin.class, ITransacte.class})
    public CpMsgTemplete update(@Description("消息对象") CpMsgTemplete cpMsgTemplete){
        cpMsgTemplete.setMtime(DateUtils.getTimeNow());
        cpMsgTempleteMapper.updateTemplateById(cpMsgTemplete);
        return cpMsgTemplete;
    }

    @DeleteAction("delete.json")
    @Description("删除")
    @With({ICLogin.class, ITransacte.class})
    public Integer delete(@Description("消息主键") String mtid){
        Integer count = cpMsgTempleteMapper.deleteSingleById(mtid);
        return count;
    }

    @PostAction("send.json")
    @Description("发送")
    @With({ICLogin.class, ITransacte.class})
    public Integer send(@Description("消息主键") String mtid){
        CpMsgTemplete cpMsgTemplete = cpMsgTempleteMapper.single(mtid);
        if(S.eq(cpMsgTemplete.getIssend(),GlobalValues.FLAG_YES.value())){
            throw new UserException("该消息已经发送，无需重复发送");
        }
        cpMsgTempleteMapper.insertMsgByMsgTemplete(mtid,cpMsgTemplete.getSendlevel(),DateUtils.getTimeNow());
        sendMsg(mtid);
        return 1;
    }

    //分页查询
    private void sendMsg(String mtid){
        List<CpMsg> list = cpMsgTempleteMapper.selectAllMsgToSend(mtid);
        list.forEach( msg->WsService.handleNewsUpdate(msg));
    }

}
